#region File Description
//-----------------------------------------------------------------------------
// BackgroundScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Outliers;
#endregion

namespace OutliersSample
{
    /// <summary>
    /// The background screen sits behind all the other menu screens.
    /// It draws a background image that remains fixed in place regardless
    /// of whatever transitions the screens on top of it may be doing.
    /// </summary>
    class SplashScreenTwo : GameScreen
    {
        #region Fields

        ContentManager content;
        Texture2D backgroundTexture;
        //How long should the screen stay fully visible
        const float timeToStayOnScreen = 3.5f;
        //Keep track of how much time has passed
        float timer = 0f;
        #endregion

        #region Initialization


        /// <summary>
        /// Constructor.
        /// </summary>
        public SplashScreenTwo()
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }


        /// <summary>
        /// Loads graphics content for this screen. The background texture is quite
        /// big, so we use our own local ContentManager to load it. This allows us
        /// to unload before going from the menus into the game itself, wheras if we
        /// used the shared ContentManager provided by the Game class, the content
        /// would remain loaded forever.
        /// </summary>
        public override void Activate(bool instancePreserved)
        {
            if (!instancePreserved)
            {
                if (content == null)
                    content = new ContentManager(ScreenManager.Game.Services, "Content");

                backgroundTexture = content.Load<Texture2D>("AJeremiahIsaacsonProductionFunkDeStihl");
            }
        }


        /// <summary>
        /// Unloads graphics content for this screen.
        /// </summary>
        public override void Unload()
        {
            content.Unload();
        }


        #endregion

        #region Update and Draw


        /// <summary>
        /// Updates the background screen. Unlike most screens, this should not
        /// transition off even if it has been covered by another screen: it is
        /// supposed to be covered, after all! This overload forces the
        /// coveredByOtherScreen parameter to false in order to stop the base
        /// Update method wanting to transition off.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
           // base.Update(gameTime, otherScreenHasFocus, false);

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            if (ScreenState == ScreenState.Active)
            {
                //When this screen is fully active, we want to
                //begin our timer so we know when to fade out
                float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
                timer += elapsed;
                if (timer >= timeToStayOnScreen)
                {
                    //When we've passed the 'timeToStayOnScreen' time,
                    //we call ExitScreen() which will fade out then
                    //kill the screen afterwards
                    ExitScreen();
                }
            }
            else if (ScreenState == ScreenState.TransitionOff)
            {
                if (TransitionPosition == 1)
                {
                    //When 'TransistionPosition' hits 1 then our screen
                    //is fully faded out. Anything in this block of
                    //code is the last thing to be called before this
                    //screen is killed forever so we add the next screen(s)
                    ScreenManager.AddScreen(new BackgroundScreen(), null);
                    ScreenManager.AddScreen(new PhoneMainMenuScreen(), null);
                }
            }
        }


        /// <summary>
        /// Draws the background screen.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);

            spriteBatch.Begin();

            spriteBatch.Draw(backgroundTexture, fullscreen,
                             new Color(TransitionAlpha, TransitionAlpha, TransitionAlpha));

            spriteBatch.End();
        }


        #endregion
    }
}
